package me.weijing.common.http;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;

import android.text.TextUtils;

public class HttpRequestJsonPost extends HttpRequest{
	private JSONObject jsonParam;
	public HttpRequestJsonPost(String origUrl, String url, HttpCallback callback,
			Map<String, String> extraHeader, JSONObject jsonParam) {
		super(origUrl, url, callback, extraHeader, null);
		this.jsonParam=jsonParam;
	}

	@Override
	public void execute() {
		URL myUrl = null;
		HttpURLConnection conn = null;
		OutputStream os = null;
		InputStream in = null;
		InputStreamReader reader = null;
		ByteArrayOutputStream bos = null;
		boolean mark = false;
		if(TextUtils.isEmpty(url) ||null==this.jsonParam){
			if(callback != null){
				callback.interpret(null);
			}
			return;
		}
		try {		
			myUrl = new URL(url);
			conn = (HttpURLConnection) myUrl.openConnection();
			conn.setDoInput(true);// 允许输入
			conn.setDoOutput(true);// 允许输出
			conn.setUseCaches(false);// 不使用Cache
			conn.setRequestMethod("POST");		
			if(mExtraHeader != null){
				Iterator<Map.Entry<String,String>> itr=mExtraHeader.entrySet().iterator();
				while(null != itr && itr.hasNext()){
					Map.Entry<String,String> entry=itr.next();
					conn.setRequestProperty(entry.getKey(),entry.getValue());
				}
			}
			 
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-type", "application/binary");
			
			conn.setConnectTimeout(TIMEOUT_CONNECT);
			conn.setReadTimeout(TIMEOUT_READ);
			conn.connect();
			// body
			os = conn.getOutputStream();
			os.write(this.jsonParam.toString().getBytes());
			os.flush();
			int responseCode = conn.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				bos = new ByteArrayOutputStream();
				in = conn.getInputStream();
				byte[] data = null;
			    if(data == null){
                    data = new byte[1024];
                }
			    int icount = 0;
			    while(true){
			        int iret=in.read(data);
			        if(iret<0)
			            break;
			        bos.write(data, 0, iret);
			        icount++;
					if(icount > UPLOAD_FILE_SIZE){
						bos.flush();
						icount=0;
					}
			    }
				bos.flush();
				if(callback != null){
					mark = true;
					callback.interpret(bos.toByteArray());
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}catch(Throwable e){
			e.printStackTrace();
		}finally {
			if(bos != null){
				try {
					bos.close();
				} catch (Throwable e) {
					 
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (Throwable e) {
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (Throwable e) {
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (Throwable e) {
				}
			}
			if (conn != null) {
				try{
				conn.disconnect();
				}catch(Throwable t){
					t.printStackTrace();
				}
				conn = null;
			}
			if(!mark && callback != null){
				callback.serverFail(AsyncHttpRequestBase.FAILTYPE_SERVER_FAIL);
			}
		}
		
	}

}
